package cn.casc.tools.maven;

import com.alibaba.fastjson.JSON;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.Charset;
import java.util.logging.Logger;

/**
 * @author max
 * @version 1.0.1
 * @className Config
 * @description
 * @copyright Copyright (c) 2023
 * @company 天纲梦工厂
 * @datetime 2024/7/8 10:15
 **/
public class Config {

    final static Logger LOGGER = Logger.getLogger(Config.class.getName());
    private static Config config;

    public volatile String lastOpenDir = "";
    public volatile String zipPassword;
    public volatile String snapshotMvnCmd = "mvn deploy:deploy-file -DgroupId=%s -DartifactId=%s -Dversion=%s -Dfile=%s" +
            " -s settings.xml -Durl=http://192.168.5.120:8081/repository/maven-snapshots/ -DrepositoryId=snapshots";
    public volatile String releaseMvnCmd = "mvn deploy:deploy-file -DgroupId=%s -DartifactId=%s -Dversion=%s -Dfile=%s"
            + " -s settings.xml -Dpackaging=jar -Durl=http://192.168.5.120:8081/repository/maven-releases/" +
            " -DrepositoryId=releases";


    public synchronized static Config load() {
        try (FileInputStream fos = new FileInputStream("config.json")){
            byte[] buf = new byte[8192];
            int len = fos.read(buf);
            config = JSON.parseObject(buf, 0, len, Charset.defaultCharset(), Config.class);
        } catch (Exception e) {
            e.printStackTrace();
            config = new Config();
        }
        return config;
    }

    public void save() {
        try (FileOutputStream fos = new FileOutputStream("config.json")){
            String config = JSON.toJSONString(this,true);
            fos.write(config.getBytes());
        } catch (Exception e) {
            e.printStackTrace();
            LOGGER.info(e.getMessage());
        }
        LOGGER.info("Save config.json");
    }

    public static Config get() {
        return config;
    }


}
